package com.igeek.dao;

import com.igeek.domain.Order;
import com.igeek.domain.OrderItem;
import com.igeek.domain.Product;
import com.igeek.utils.DataSourceUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.sql.SQLException;
import java.util.List;

/**
 * @author jerry
 * @Company 极客营
 */
public class OrderDao {
    private ProductDao productDao=new ProductDao();

    /**
     * 向订单表中插入数据
     * @param order
     * @return
     * @throws SQLException
     */
    public void insertOrder(Order order) throws SQLException {
        QueryRunner qr=new QueryRunner();
        String sql="insert into orders values (?,?,?,?,?,?,?,?)";
        Object [] params={order.getOid(),order.getOrdertime(),order.getTotal(),order.getState(),order.getAddress(),
                         order.getName(),order.getTelephone(),order.getUser().getUid()};
        qr.update(DataSourceUtils.getConnection(),sql,params);
    }

    /**
     * 插入订单详情表
     * @param order
     * @return
     */
    public void insertOrderItem(Order order) throws SQLException {
        QueryRunner qr=new QueryRunner();
        List<OrderItem> list=order.getOrderItems();
        for(int i=0;i<list.size();i++){
            OrderItem item=list.get(i);
            String sql="insert into orderitem values (?,?,?,?,?)";
            Object [] params={item.getItemid(),item.getCount(),item.getSubtotal(),item.getProduct().getPid(),order.getOid()};
            qr.update(DataSourceUtils.getConnection(),sql,params);
        }
    }

    /**
     * 查询所有的订单
     * @return
     */
    public List<Order> findAllOrder(String uid) throws SQLException {
        QueryRunner qr=new QueryRunner(DataSourceUtils.getDataSource());
        String sql="select * from orders where uid=?";
        Object [] params={uid};
        List<Order> orderList=qr.query(sql,new BeanListHandler<>(Order.class),params);
        if(orderList!=null){
            for (Order order:orderList){
                List<OrderItem> orderItemList=findAllOrderItem(order.getOid());
                //存放到对应的订单对象中
                order.setOrderItems(orderItemList);
            }
        }
        return orderList;
    }

    /**
     * 查询某订单下的所有订单详情
     * @return
     */
    public List<OrderItem> findAllOrderItem(String oid) throws SQLException {
        QueryRunner qr=new QueryRunner(DataSourceUtils.getDataSource());
        String sql="select * from orderitem where oid=?";
        Object [] params={oid};
        List<OrderItem> orderItemList=qr.query(sql,new BeanListHandler<>(OrderItem.class),params);
        for(OrderItem orderItem:orderItemList){
            Product product=productDao.findProductByPid(orderItem.getPid());
            orderItem.setProduct(product);
        }
        return orderItemList;
    }

    public Order findOrderDetail(String oid) throws SQLException {
        QueryRunner qr=new QueryRunner(DataSourceUtils.getDataSource());
        String sql="select * from orders where oid=?";
        Object [] params={oid};
        Order order=qr.query(sql,new BeanHandler<>(Order.class),params);
        List<OrderItem> list=findAllOrderItem(oid);
        order.setOrderItems(list);
        return order;
    }

    public void updateOrder(Order order) throws SQLException {
        QueryRunner qr=new QueryRunner(DataSourceUtils.getDataSource());
        String sql="update orders set address=?,name=?,telephone=? where oid=?";
        Object [] params={order.getAddress(),order.getName(),order.getTelephone(),order.getOid()};
        qr.update(sql,params);
    }

    public void ConfirmOrder(Order order) throws SQLException {
        QueryRunner qr=new QueryRunner(DataSourceUtils.getDataSource());
        String sql="update orders set state=1 where oid=?";
        Object [] params={order.getOid()};
        qr.update(sql,params);
    }
}
